package com.cinsea.mobile.ext;

import com.alibaba.fastjson.JSON;
import com.cinsea.common.Env;
import com.cinsea.common.ExtAction;
import com.cinsea.common.JdbcDao;
import com.cinsea.common.Page;
import com.cinsea.hrm.service.HumanService;
import com.cinsea.hrm.service.OrgService;
import com.cinsea.interfaces.model.Cell;
import com.cinsea.interfaces.model.Dataset;
import com.cinsea.interfaces.workflow.RequestInfo;
import com.cinsea.interfaces.workflow.WorkflowServiceImpl;
import com.cinsea.security.util.JdbcUtil;
import com.cinsea.util.DateUtil;
import com.cinsea.util.NumberUtil;
import com.cinsea.util.SqlUtil;
import com.cinsea.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: xf
 * Date: 13-7-21
 * Time: 下午5:04
 */
public class HrmAction extends ExtAction {
    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String action = StringUtil.null2String(request.getParameter("action"));
        String key = StringUtil.null2String(request.getParameter("q"));
        int page = NumberUtil.getIntegerValue(request.getParameter("page"), 1);
        JdbcDao jdbcDao = Env.getBean("jdbcDao");
        OrgService orgService = Env.getBean("orgService");
        if (action.equals("gethumanlist")) {
            StringBuffer sql = new StringBuffer("select id,objname,orgid,tel2 from cs_human where deleted=0 and 1=1 ");
            ArrayList sqlparam = new ArrayList();
            if (!StringUtil.isEmpty(key)) {
                sql.append(" and objname like ?");
                sqlparam.add("%" + key + "%");
            }
            sql.append(" order by objname ");
            Page pageObject = jdbcDao.pagedQuery(sql.toString(), sqlparam.toArray(), page, 10);
            if (pageObject.getTotalSize() != 0) {
                List<Map> result = (List) pageObject.getResult();
                List jsonlist = new ArrayList();
                for (Map obj : result) {
                    Map rowdata = new HashMap();
                    rowdata.put("id", obj.get("id"));
                    rowdata.put("processid", obj.get("id"));
                    rowdata.put("name", obj.get("objname"));
                    rowdata.put("cell", obj.get("tel2"));
                    String org = orgService.get(StringUtil.null2String(obj.get("orgid"))).getObjname();
                    rowdata.put("org", org);
                    jsonlist.add(rowdata);
                }
                Map object = new HashMap();
                object.put("totalCount", pageObject.getTotalSize());
                object.put("result", jsonlist);
                response.getWriter().print(JSON.toJSONString(object));
            } else {
                Map object = new HashMap();
                object.put("totalCount", 0);
                gzip(request,response,JSON.toJSONString(object));
            }
        }


        if (action.equals("saveliuyan")) {
            String content = StringUtil.null2String(request.getParameter("content"));
            String processid = StringUtil.null2String(request.getParameter("processid"));
            String type = StringUtil.null2String(request.getParameter("type"));
            String lyer = Env.getRemoteUser().getId();
            String lydate = DateUtil.getCurrentDate();
            WorkflowServiceImpl si = new WorkflowServiceImpl();
            RequestInfo formdata = new RequestInfo();
            formdata.setOperator(Env.getRemoteUser().getId());
            //formdata.setTitle(StringUtil.lift(content,10));
            formdata.setTypeid("402882e42d5a8fdc012d5b39ba110047"); //留言流程id
            String crmid = "";
            Dataset ds = new Dataset();
            List<Cell> mainlist = new ArrayList(); //主表单元格
            Cell c1 = new Cell();
            c1.setId("ut_liuyan_content");//留言内容
            c1.setValue(content);
            Cell c2 = new Cell();
            c2.setId("ut_liuyan_liuyanren");//留言人
            c2.setValue(lyer);
            Cell c3 = new Cell();
            c3.setId("ut_liuyan_liuyanriqi");//留言日期
            c3.setValue(lydate);

            Cell c4 = new Cell();
            c4.setId("ut_liuyan_blyr");//被留言人
            c4.setValue(processid);

            Cell c5 = new Cell();
            c5.setId("ut_liuyan_title");//标题
            c5.setValue(StringUtil.lift(content,10));
            mainlist.add(c1);
            mainlist.add(c2);
            mainlist.add(c3);
            mainlist.add(c4);
            mainlist.add(c5);
            ds.setMaintable(mainlist);
            formdata.setData(ds);
            si.createRequest(formdata);
            response.getWriter().print("{\"success\":true}");
        }
    }
}
